﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;

namespace UnifiedTool.ViewModel
{
    public static class MediaElementHelper
    {
        public static readonly DependencyProperty GetPositionProperty = DependencyProperty.RegisterAttached(
            "GetPosition",
            typeof(TimeSpan),
            typeof(MediaElementHelper),
            new PropertyMetadata(TimeSpan.Zero, OnGetPositionChanged));

        public static readonly DependencyProperty SetPositionProperty = DependencyProperty.RegisterAttached(
            "SetPosition",
            typeof(TimeSpan),
            typeof(MediaElementHelper),
            new PropertyMetadata(TimeSpan.Zero, OnSetPositionChanged));

        public static TimeSpan GetGetPosition(DependencyObject obj)
        {
            return (TimeSpan)obj.GetValue(GetPositionProperty);
        }

        public static void SetGetPosition(DependencyObject obj, TimeSpan value)
        {
            obj.SetValue(GetPositionProperty, value);
        }

        public static TimeSpan GetSetPosition(DependencyObject obj)
        {
            return (TimeSpan)obj.GetValue(SetPositionProperty);
        }

        public static void SetSetPosition(DependencyObject obj, TimeSpan value)
        {
            obj.SetValue(SetPositionProperty, value);
        }

        private static void OnGetPositionChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var mediaElement = obj as MediaElement;
            if (mediaElement != null && !mediaElement.Position.Equals(e.NewValue))
            {
                mediaElement.Position = (TimeSpan)e.NewValue;
            }
        }

        private static void OnSetPositionChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var mediaElement = obj as MediaElement;
            if (mediaElement != null)
            {
                mediaElement.SetValue(GetPositionProperty, e.NewValue);
            }
        }
    }

}
